package com.bwsoft.admin.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.dao.NewsDao;
import com.bwsoft.admin.domain.News;
import com.bwsoft.admin.domain.NewsCategory;
import com.bwsoft.admin.domain.valueObject.NewsVO;

public class NewsAction extends BaseAction_ybd {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5289128443450825119L;
	private static Logger LOG = Logger.getLogger(NewsAction.class);

	private String status;
	private String lunboFlag;
	private String s_category;
	private String s_lunboFlag;
	private String s_title;
	private String s_status;
	//查询和新增的celect所需数据
	private  List<NewsCategory> newsCategoryList;
	public String newsIndex(){
		//查询所有newsCategory的值
		newsCategoryList = newsCategoryService.findAll();
		return "newsIndex";
	}
	
	public List<NewsCategory> getNewsCategoryList() {
		return newsCategoryList;
	}

	public void setNewsCategoryList(List<NewsCategory> newsCategoryList) {
		this.newsCategoryList = newsCategoryList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getLunboFlag() {
		return lunboFlag;
	}

	public void setLunboFlag(String lunboFlag) {
		this.lunboFlag = lunboFlag;
	}

	public String getS_category() {
		return s_category;
	}

	public void setS_category(String s_category) {
		this.s_category = s_category;
	}

	public String getS_lunboFlag() {
		return s_lunboFlag;
	}

	public void setS_lunboFlag(String s_lunboFlag) {
		this.s_lunboFlag = s_lunboFlag;
	}

	public String getS_title() {
		return s_title;
	}

	public void setS_title(String s_title) {
		this.s_title = s_title;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	/**
	 * 资讯查询获取列表
	 */
	public String getNewsJson() {

		int count = 0;
		List<News> NewsSet=null;
		
		//通过字典编号查询字典详情的分页集合
		NewsSet = newsService.findData(page, rows, s_category,  s_lunboFlag, s_title, s_status);
		//通过编号查询字典集合的大小	
		count = newsService.getTotalCount(s_category,  s_lunboFlag, s_title, s_status);
//			转成Vo
		List<NewsVO> newsVo = newsService.getVo(NewsSet);
		
		responseJson.put("total", count);
		responseJson.put("rows", newsVo);

		return "mapJsonResult";
	}
	
	/**
	 * 资讯新增
	 * @return
	 */
	public String saveNews(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		try{
		int count_lunbo = 0;
		news.setStatus(status==null ? false:status.equals("1"));
		news.setLunboFlag(lunboFlag==null ? false:lunboFlag.equals("1"));
		List<News> NewsSet=null;
		//查处现有的轮播图的个数
		NewsSet = newsService.findAll(news.getCategory().getId());
		for (News news : NewsSet) {
			if(news.getStatus() && news.isLunboFlag()){
				count_lunbo++;
			}
		}
		
		if(suolveImg != null && !"".equals(suolveImg) && newsService.isImage(suolveImgType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "缩略图上传的文件不是图片类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "缩略图上传的文件不是图片类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(suolveImg != null && !"".equals(suolveImg) && !newsService.isFitSize(suolveImg)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "缩略图上传的图片不能超过8M！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "缩略图上传的图片不能超过8M！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		
		if(lunboImg != null && !"".equals(lunboImg) && newsService.isImage(lunboImgType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "轮播图上传的文件不是图片类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "轮播图上传的文件不是图片类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(lunboImg != null && !"".equals(lunboImg) && !newsService.isFitSize(lunboImg)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "轮播图上传的图片不能超过8M！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "轮播图上传的图片不能超过8M！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		
		if(news.getStatus()&&news.isLunboFlag()){
			if(count_lunbo>=3){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "同类别下的资讯轮播图不得超过三个！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "同类别下的资讯轮播图不得超过三个！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
		}
		
		msg = newsService.saveNews(news,suolveImg,suolveImgFileName,lunboImg,lunboImgFileName);
		}catch (Exception e) {
			e.printStackTrace();		
		}
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 资讯保存
	 * @return
	 */
	public String updateNews(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		try{
	
			int count_lunbo = 0;
			news.setStatus(status==null ? false:status.equals("1"));
			news.setLunboFlag(lunboFlag==null ? false:lunboFlag.equals("1"));
			List<News> NewsSet=null;
			//查处现有的轮播图的个数
			NewsSet = newsService.findAll(news.getCategory().getId());
			for (News oldNews : NewsSet) {
				if(oldNews.getStatus() && oldNews.isLunboFlag()){
					if(news.getNewsId().equals(oldNews.getNewsId())){
						continue;
					}
					count_lunbo++;
				}
			}
		
		if(news.getStatus()&&news.isLunboFlag()){
			if(count_lunbo>=3){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "同类别下的资讯轮播图不得超过三个！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "同类别下的资讯轮播图不得超过三个！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
		}
		if(suolveImg != null && !"".equals(suolveImg) && newsService.isImage(suolveImgType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "缩略图上传的文件不是图片类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "缩略图上传的文件不是图片类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(suolveImg != null && !"".equals(suolveImg) && !newsService.isFitSize(suolveImg)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "缩略图上传的图片不能超过8M！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "缩略图上传的图片不能超过8M！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(lunboImg != null && !"".equals(lunboImg) && newsService.isImage(lunboImgType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "轮播图上传的文件不是图片类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "轮播图上传的文件不是图片类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(lunboImg != null && !"".equals(lunboImg) && !newsService.isFitSize(lunboImg)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "轮播图上传的图片不能超过8M！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "轮播图上传的图片不能超过8M！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		msg = newsService.updateNews(news,suolveImg,suolveImgFileName,lunboImg,lunboImgFileName);
		}catch (Exception e) {
			e.printStackTrace();		
		}
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 资讯删除
	 * @return
	 */
	public String removeNews(){
		msg = newsService.removeNews(news.getNewsId());
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 修改前获取资讯内容
	 * @return
	 */
	public String newsModifyInfo(){
		news=newsService.newsModifyInfo(news.getNewsId());
		
		List<News> newsList = new ArrayList<News>();
		newsList.add(news);
		List<NewsVO> listVo = newsService.getVo(newsList,"needContent");
		
		responseJson.put("news", listVo.get(0));
		return "mapJsonResult";
	}
	/**
	 *  浏览资讯内容效果
	 * @return
	 */
	public String contentShow(){
		news=newsService.newsModifyInfo(news.getNewsId());
		return "contentShow";
	}
	
}
